{pathname} = location
pathname = pathname[1..]
if $('a[data-portal=signup]')[0]
  if pathname
    localStorage.url = pathname
else
  if not pathname.startsWith 'members/'
    {url} = localStorage
    if url
      if not pathname
        location.href = '/'+url
      delete localStorage.url

SUPPORT_WEBP = !![].map && document.createElement('canvas').toDataURL('image/webp').indexOf('data:image/webp') == 0

PROXY=location.protocol+"//blog-img.xvc.com/0/"
origin=location.origin+"/"

$('.gh-content img').each ->
    src=@src
    if not src or src.indexOf("://")<0
        return

    if not SUPPORT_WEBP
        # 微信公众号图片
        src = src.replace("=webp","=jpg").replace("%3Dwebp","%3Djpg")
    if not src.split("/")[2].endsWith(".xvc.com")
        src = PROXY+encodeURIComponent(src)
    if src!=@src
        @src=src

if navigator.userAgent.indexOf('MicroMessenger')+1
  $.getScript("//blog-js.xvc.com/wx.js?a")

mail = undefined

window._hmt = window._hmt or []

get_mail = =>
  if mail
    return mail

  mail = await (await fetch('/members/api/member')).text()
  if mail
    mail = JSON.parse(mail).email
    _hmt.push(['_setUserId',mail])
  mail

window.addEventListener(
  'message',
  (e)=>
    if e.data=='mail'
      await get_mail()
      if mail
        e.source.postMessage(mail,'*')
    return
)


window.addEventListener 'load',=>
  for iframe from document.getElementsByTagName('iframe')
    if iframe.src.startsWith 'https://blog-play.xvc.com/'
      await get_mail()
      iframe.contentWindow.postMessage(mail,'*')
  ir_a = $('a.ir')[0]
  if ir_a
    {href} = ir_a
    ir_a.href = href.slice(0,7)+encodeURI(decodeURI(ir_a.href.slice(7)).replace('/ ',location.pathname+' '))
  return

Script = (src) ->
  hm = document.createElement('script')
  hm.src = '//' + src
  s = document.getElementsByTagName('script')[0]
  s.parentNode.insertBefore hm, s
  return

window._paq = window._paq = window._paq || []
_paq.push(['trackPageView'])
_paq.push(['enableLinkTracking'])
_paq.push(['setSiteId', '2'])
u="//tj.xvc.com/"
_paq.push(['setTrackerUrl', u+'matomo.php'])
Script u+'tj'
